package com.yanzhenjie.andserver.sample.interceptor;

import android.content.res.AssetManager;

import com.yanzhenjie.andserver.RequestHandler;
import com.yanzhenjie.andserver.interceptor.Interceptor;
import com.yanzhenjie.andserver.sample.handler.AssetsFileHandler;
import com.yanzhenjie.andserver.sample.handler.anjian.DTwitterListV6Handler;
import com.yanzhenjie.andserver.sample.handler.anjian.GetRunPermHandler;
import com.yanzhenjie.andserver.sample.handler.anjian.PacAssetFileHandler;
import com.yanzhenjie.andserver.sample.handler.anjian.ProxyHandler;
import com.yanzhenjie.andserver.sample.handler.anjian.ToolHeartbeatHandler;
import com.yanzhenjie.andserver.sample.handler.anjian.UserInfoHandler;
import com.yanzhenjie.andserver.util.AssetsReader;

import org.apache.httpcore.HttpException;
import org.apache.httpcore.HttpRequest;
import org.apache.httpcore.HttpResponse;
import org.apache.httpcore.protocol.HttpContext;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/13.
 */

public class DefaultInterceptor implements Interceptor {

    private Map<String, RequestHandler> mRequestHandlerMap;
    private RequestHandler defaultRequestHandler;
    private AssetsReader mAssetsReader;
    private RequestHandler defaultFileHandler;

    public DefaultInterceptor(){
        mRequestHandlerMap = new HashMap<String, RequestHandler>();

        registerHandler("/api/DTwitterListV6", new DTwitterListV6Handler())
        .registerHandler("/api/GetRunPerm", new GetRunPermHandler())
        .registerHandler("/api/ToolHeartbeat", new ToolHeartbeatHandler())
        .registerHandler("/api/UserInfo", new UserInfoHandler());
/*        registerHandler("/download", new FileHandler())
        .registerHandler("/login", new LoginHandler())
        .registerHandler("/upload", new UploadHandler())
        .registerHandler("/image", new ImageHandler());*/

//        defaultRequestHandler = new DefaultHandler();
        defaultRequestHandler = new ProxyHandler();
    }

    public DefaultInterceptor(AssetManager assetManager){
        this();
        defaultFileHandler = new AssetsFileHandler(assetManager);
        mAssetsReader = new AssetsReader(assetManager);

        registerHandler("/anjian.pac", new PacAssetFileHandler(assetManager));
    }

    private DefaultInterceptor registerHandler(String path, RequestHandler handler) {
        this.mRequestHandlerMap.put(path, handler);
        return this;
    }

    private RequestHandler getDefaultRequestHandler(){
        return defaultRequestHandler;
    }

    private RequestHandler getRequestHandler(String path){
        RequestHandler handler = mRequestHandlerMap.get(path);
/*        if(null == handler){
            handler = getDefaultRequestHandler();
        }*/
        if((null == handler) && path.startsWith("/api")){
            handler = getDefaultRequestHandler();
        }
        return handler;
    }

    public boolean onBeforeExecute(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
        String uri = httpRequest.getRequestLine().getUri();//It's different
        String path = uri;
        if(!uri.startsWith("/")){
            URL url = new URL(uri);
            path = url.getPath();
        }
/*        if(mAssetsReader.isFile("web"+path)){
            defaultFileHandler.handle(httpRequest,httpResponse,httpContext);
        }else{*/
            RequestHandler handler = getRequestHandler(path);
            if(null == handler){
                return false;
            }else{
                handler.handle(httpRequest,httpResponse,httpContext);
                return true;
            }
//        }
    }

    public void onAfterExecute(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {

    }
}
